<template>
  <transition name="v-modal">
    <div
      class="v-modal-mask"
      :class="{'v-modal-shade': shade}"
      v-if="currentValue"
      @touchmove.prevent
    >
      <input v-model="currentValue" class="hide" />
      <div class="v-modal-wrapper">
        <div class="v-modal-container">
          <div class="v-modal-header">
            {{title}}
          </div>
          <div class="v-modal-body" @touchmove.stop>
            <slot></slot>
          </div>
          <div class="v-model-footer">
            <div class="v-modal-vertial-line" v-if="cancelText && okText"></div>
            <div class="table-row">
              <div class="cell block text-center" v-if="cancelText">
                <button
                  class="v-button v-button-flat v-button-block v-button-ghost-default"
                  @click="onCancel"
                >
                  {{cancelText}}
                </button>
              </div>
              <div class="cell block text-center" v-if="okText">
                <button
                  class="v-button v-button-flat v-button-block v-button-ghost-danger"
                  @click="onConfirm"
                  :disabled="confirmDisabled"
                >
                  {{okText}}
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'Modal',
  props: {
    shade: Boolean,
    title: String,
    cancelText: String,
    okText: String,
    confirmDisabled: Boolean,
    value: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      currentValue: this.value,
    }
  },
  created() {
    if (typeof this.value !== 'undefined') {
      this.currentValue = this.value
    }
  },
  watch: {
    value(val) {
      this.currentValue = val
    },
    currentValue(val) {
      this.$emit('input', val)
    },
  },
  methods: {
    onCancel() {
      this.currentValue = false
      console.log(this.currentValue)
      this.$emit('on-cancel')
    },
    onConfirm() {
      this.currentValue = false
      this.$emit('on-confirm')
    },
  },
}
</script>

<style lang="scss">
@import "~styles/include";

.v-modal-mask {
  position: fixed;
  display: table;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  overflow: hidden;
}

.v-modal-shade {
  background-color: rgba(0, 0, 0, .5);
  transition: opacity .3s ease;
}

.v-modal-wrapper {
  display: table-cell;
  vertical-align: middle;
}

.v-modal-container {
  width: 280px;
  background-color: #fff;
  margin: 0 auto;
  border-radius: 4px;
  box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.22), 0 6px 6px 0 rgba(0, 0, 0, 0.30);
  transition: all .3s ease;
}

.v-modal-body {
  overflow-y: auto;
  max-height: 300px;
  -webkit-overflow-scrolling: touch;
}

.v-modal-header {
  line-height: 48px;
  font-size: 16px;
  text-align: center;
  border-bottom: 1px solid $greyLineColor;
}

.v-model-footer {
  border-top: 1px solid $greyLineColor;
  height: 48px;
  position: relative;
}

.v-modal-vertial-line {
  position: absolute;
  top: 0;
  left: 50%;
  width: 1px;
  height: 100%;
  background-color: $greyLineColor;
}

.v-modal-enter {
  opacity: 0;
}

.v-modal-leave-active {
  opacity: 0;
}

.v-modal-enter .v-modal-container,
.v-modal-leave-active .v-modal-container {
  transform: scale(1.1);
}
</style>
